<?php

namespace App\Admin\Controllers\Finance;

use App\Admin\Exports\StatDailyExport;
use App\Http\Controllers\Controller;
use App\Model\PayOrder;
use App\Model\Stat\StatDaily;
use App\Model\UserCoin;
use App\Model\Withdraw;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;

class DailyInfoController extends Controller
{
    use ModelForm;

    public function index()
    {
        $this->adminId = Admin::user()->id;
        $content = new Content();
        // $remain = UserCoin::getRemainCoin();
        $content->header('基础数据日报');
        // $content->description('剩余待提现：' . $remain);
        $content->body($this->grid());
        return $content;
    }

    protected function grid()
    {
        $grid = new Grid(new StatDaily());
        if (!request()->dt) {
            request()->merge(['dt' => ['start' => date('Y-m-d', strtotime('-30 day')), 'end' => '']]);
        }
        // if (!request()->channel) {
        //     request()->merge(['channel' => 'all']);
        // }
        $where = [
            ['channel', 'all']
        ];
        $grid->model()->where($where)->orderBy('dt', 'desc')->orderBy('channel', 'desc');
        $grid->dt('日期');
        $grid->recharge_rmb('充值（元）')->display(function ($data) {
            return round($data / 100, 1);
        })->totalRow(function ($amount) {
            $this->total_rmb = $amount / 100;
            return "{$this->total_rmb} 元";
        });
        // ->totalRow(function ($amount) {
        //     $amount = $amount / 100;
        //     return "{$amount} 元";
        // });
        $grid->recharge_coin('充值（金币）')->totalRow(function ($amount) {
            $this->total_coin = $amount;
            return "{$amount} 金币";
        });
        $grid->free_income_coin('福利收入（金币）')->totalRow(function ($amount) {
            return "{$amount} 金币";
        });
        $grid->send_coin('充值赠送（金币）')->totalRow(function ($amount) {
            return "{$amount} 金币";
        });
        $grid->income_coin('女生收入（金币）')->totalRow(function ($amount) {
            return "总收入：{$amount}";
        });
        $grid->withdraw_rmb('申请提现（元）')->display(function ($data) {
            if ($this->dt >= '2022-02-01') {
                return round($this->withdraw_coin * 0.1 * config('busi.withdraw_percent'), 1);
            }
            return round($this->withdraw_coin * 0.1, 1);
        });
        $grid->withdraw_coin('申请提现（金币）');
        // $grid->disableExport();
        $grid->disableRowSelector();
        $grid->disableActions();
        $grid->disableCreateButton();
        $grid->exporter(new StatDailyExport($grid));
        $grid->filter(function ($filter) {
            $filter->expand();
            $filter->disableIdFilter();
            $filter->column(1 / 3, function ($filter) {
                $filter->between('dt', '日期')->date();
            });
            // $filter->column(1 / 3, function ($filter) {
            //     $filter->in('channel', '渠道')->multipleSelect('/get_channels?channel=all');
            // });
        });
        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableDelete();
            $actions->disableEdit();
            $actions->disableView();
        });

        $grid->footer(function ($query) {
            $date = date('Y-m-d', strtotime('-40 day'));
            $remain = UserCoin::getRemainCoin($date);
            $withdrawUnPay = Withdraw::getUnPay();
            $withdrawCoin = Withdraw::getPay();
            $payCoin = PayOrder::getTotal('2022-06-01', 'coin');
            return '<table class="table custom-data-table data-table">
            <thead>
            <tr>
                <th>充值+赠送（金币）：' . $payCoin . '</th>
            </tr>
            <tr>
                <th>未申请提现（金币）：' . $remain . '</th>
            </tr>
            <tr>
                <th>已申请已打款（金币）：' . $withdrawCoin . '</th>
            </tr>
            <tr>
                <th>已申请未打款（金币）：' . $withdrawUnPay . '</th>
            </tr>
            </thead></table>';
        });
        return $grid;
    }
}
